package pl.edu.agh.iosr.gamblingzone.model;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

/**
 * The Class BettingSlip.
 *
 * 
 * @author maciej
 */
@AttributeOverride(name = "id", column = @Column(name = "BETTING_SLIP_ID"))
@Entity(name = "BETTING_SLIPS")
@Inheritance(strategy = InheritanceType.JOINED)
public class BettingSlip extends EntityModel {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -7277625657154685200L;

	/** The money. */
	@Column(name = "BETTING_SLIP_MONEY", nullable = false)
	private Long money = 0L;

	/** The was paid. */
	@Column(name = "BETTING_SLIP_WAS_PAID", nullable = false)
	private boolean wasPaid = false;

	@Column(name = "BETTING_SLIP_CREATE_DATE", nullable = false)
	private Date creationDate = new Date();

	/** The owner. */
	@ManyToOne
	@JoinTable(name = "USER_BETTINGSLIP", joinColumns = { @JoinColumn(name = "BETTING_SLIP_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") })
	private User owner;

	/** The bets. */
	@OneToMany(mappedBy="bettingSlip", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
//	@OneToMany(mappedBy="bettingSlip", fetch=FetchType.EAGER)
//	@JoinColumn(name="BETTING_SLIP_ID")
	private List<Bet> bets = new LinkedList<Bet>();

	/**
	 * Gets the money.
	 * 
	 * @return the money
	 */
	public Long getMoney() {
		return money;
	}

	/**
	 * Sets the money.
	 * 
	 * @param money
	 *            the new money
	 */
	public void setMoney(Long money) {
		this.money = money;
	}

	/**
	 * Checks if is was paid.
	 * 
	 * @return true, if is was paid
	 */
	public boolean isWasPaid() {
		return wasPaid;
	}

	/**
	 * Sets the was paid.
	 * 
	 * @param wasPaid
	 *            the new was paid
	 */
	public void setWasPaid(boolean wasPaid) {
		this.wasPaid = wasPaid;
	}

	/**
	 * Gets the owner.
	 * 
	 * @return the owner
	 */
	public User getOwner() {
		return owner;
	}

	/**
	 * Sets the owner.
	 * 
	 * @param owner
	 *            the new owner
	 */
	protected void setOwner(User owner) {
		this.owner = owner;
	}

	/**
	 * Adds the bet.
	 * 
	 * Creates bidirectional association & automatically make Bet persistent.
	 * 
	 * @param b
	 *            the b
	 */
	public void addBet(Bet b) {
		b.setBettingSlip(this);
		this.bets.add(b);
	}

	/**
	 * Gets the bets.
	 * 
	 * @return the bets
	 */
	public List<Bet> getBets() {
		return bets;
	}

	/**
	 * Gets the creation date.
	 * 
	 * @return the creation date
	 */
	public Date getCreationDate() {
		return creationDate;
	}

	/**
	 * Sets the creation date.
	 * 
	 * @param creationDate
	 *            the new creation date
	 */
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	
}
